package com.ews.onlineexchange.service;

import java.util.List;

import javax.faces.view.ViewScoped;
import javax.inject.Named;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.ews.onlineexchange.model.AgentService;
import com.ews.onlineexchange.model.MainBusiness;
import com.ews.onlineexchange.model.ServiceSupplier;

@Named
@ViewScoped
public class ServiceSupplierAgentServiceSelectorList extends EntityQuery<ServiceSupplier> {

	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1L;

	private AgentService		agentService;

	public ServiceSupplierAgentServiceSelectorList() {
		setMaxResults( null );
	}

	@Override
	public String getOrder() {
		return "score DESC";
	}

	@Override
	protected List<Predicate> getRestrictions( CriteriaBuilder cb, Root<ServiceSupplier> entity ) {
		List<Predicate> restrictions = super.getRestrictions( cb, entity );
		Join<ServiceSupplier, MainBusiness> mainBussiness = entity.join( "mainBusinesses" );
		Join<MainBusiness, AgentService> service = mainBussiness.join( "agentService" );
		restrictions.add( cb.equal( service.get( "id" ), agentService.getId() ) );
		restrictions.add( cb.isTrue( entity.<Boolean>get( "auditted" ) ) );
		restrictions.add( cb.isTrue( entity.<Boolean>get( "valid" ) ) );

		return restrictions;
	}

	public AgentService getAgentService() {
		return agentService;
	}

	public void setAgentService( AgentService agentService ) {
		this.agentService = agentService;
	}

	@Override
	protected boolean isDistinct() {
		return true;
	}

}
